package com.atstudy.headline.web.admin.controller.users;

import com.atstudy.headline.entity.Users;
import com.atstudy.headline.enums.BaseEnum;
import com.atstudy.headline.enums.BaseStatus;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.admin.service.UsersService;
import com.atstudy.headline.web.admin.vo.users.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class usersController {

    @Autowired
    private UsersService usersService;

    /**
     * 获取分页用户信息
     * @param
     * @return
     */
    @RequestMapping("/page")
    public Result<IPage<Users>> pageUsersInfo(@RequestParam Integer current, @RequestParam Integer size, UserInfoQueryVo queryVo ) {
        Page<Users> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like( queryVo.getPhone()!=null,Users::getPhone,queryVo.getPhone() );
        queryWrapper.eq( queryVo.getStatus()!=null,Users::getStatus,queryVo.getStatus() );
        IPage<Users> usersIPage = usersService.pageInfoByUsers(page, queryVo);
        return Result.ok(usersIPage);
    }


    /**
     * 新增用户信息修改用户信息
     * @param
     * @return
     * @paramusers
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Users users) {
        usersService.saveOrUpdate(users);
        return Result.ok();
    }


    /**
     * 删除用户信息
     * @param
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer userId) {
        usersService.removeById(userId);
        return Result.ok();
    }



    /**
     * 根据id修改用户状态
     * @param
     * @return
     */
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer userId  ,@RequestParam BaseStatus status) {
        LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Users::getUserId,userId);
        updateWrapper.set(Users::getStatus,status);
        usersService.update( updateWrapper );
        return Result.ok();
    }






}
